package client;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * Date: 16-mrt-2008
 * Time: 11:24:54
 *
 * @autor Wouter Wendelen
 */
public class CssManager {

    private static CssManager instance = new CssManager();

    private Map controllers;

    private CssManager() {
        controllers = new HashMap();
    }

    public static CssManager getInstance() {
        return instance;
    }

    public void manage(String id, CssController controller) {
        controllers.put(id, controller);
    }

    public void doLink(String id, String link, String extra) {
        CssController controller = (CssController) controllers.get(id);
        if (controller != null) {
            controller.doLink(link, extra);
        }
    }

    public void doUnLink(String id) {
        CssController controller = (CssController) controllers.get(id);
        if (controller != null) {
            controller.doUnLink();
        }
    }

    public void doImg(String id, String img) {
        CssController controller = (CssController) controllers.get(id);
        if (controller != null) {
            controller.insert(img);
        }
    }

    public void doCss(String id, String cssClass) {
        CssController controller = (CssController) controllers.get(id);
        if (controller != null) {
            controller.doCss(cssClass);
        }
    }

    public String getHtml(String id) {
        CssController controller = (CssController) controllers.get(id);
        if (controller != null) {
            return controller.getHtml();
        }
        return null;
    }

    public void doPaste(String id) {
        CssController controller = (CssController) controllers.get(id);
        if (controller != null) {
            controller.doPaste();
        }
    }

    public void setHtml(String id, String html) {
        CssController controller = (CssController) controllers.get(id);
        if (controller != null) {
            controller.setHtml(html);
        }
    }

    public void boot() {
        doJsInit(this);
        for (Iterator iterator = controllers.keySet().iterator(); iterator.hasNext();) {
            String id = (String) iterator.next();
            setHtml(id, getHtmlFromJs(id));
        }
    }

    /**
     * interface with js
     */

    public native String getHtmlFromJs(String id) /*-{
        return $wnd.cssLoad(id);
    }-*/;

    public native void doJsInit(CssManager cssManager)  /*-{
        $wnd.cssClass = function(id, url) {
            cssManager.@client.CssManager::doCss(Ljava/lang/String;Ljava/lang/String;)(id,url);
        }
        $wnd.cssLink = function(id, url, extra) {
            cssManager.@client.CssManager::doLink(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(id,url,extra);
        }
        $wnd.cssUnLink = function(id) {
            cssManager.@client.CssManager::doUnLink(Ljava/lang/String;)(id);
        }
        $wnd.cssImg = function(id, img) {
            cssManager.@client.CssManager::doImg(Ljava/lang/String;Ljava/lang/String;)(id,img);
        }
        $wnd.cssSet = function(id, html) {
            return cssManager.@client.CssManager::setHtml(Ljava/lang/String;Ljava/lang/String;)(id,html);
        }
        $wnd.cssGet = function(id) {
            return cssManager.@client.CssManager::getHtml(Ljava/lang/String;)(id);
        }
        $wnd.cssPaste = function(id) {
            cssManager.@client.CssManager::doPaste(Ljava/lang/String;)(id);
        }
    }-*/;
}
